//
//  ADControl.m
//  NotificateMe
//
//  Created by jang sehoon on 11. 11. 14..
//  Copyright (c) 2011 none. All rights reserved.
//

#import "ADControl.h"
#import "GADBannerView.h"
#import "MobileAdViewController.h"

@implementation ADControl

@synthesize viewController;


- (void)dealloc{
    [viewController release];
    [adViewCtrl release];
    [adBanner release];
    [adMobBanner release];
    [super dealloc];
}

#pragma mark 초기화 

+ (ADControl *)defaultsAD{
    static ADControl *adControl = nil;
	if (!adControl) {
		adControl = [[ADControl alloc] init];
	}
	return adControl;
}

- (void)addAds:(UIViewController *)viewControl wihtYPoint:(CGFloat)y{
    yPoint = y;
    self.viewController = viewControl;
//    NSLocale *locale = [NSLocale currentLocale];
//    if ([[locale localeIdentifier] isEqualToString:@"ko_KR"]) {
                        
        //애드몹 광고 
        [self initAdmob:y];
        //다음에 아담광고 
        [self addAdamAd:y]; 
//    }
//    else{
//        
//        [self initAdmob:y];
//        
//        NSString *currentVersion = [ [ UIDevice currentDevice] systemVersion];
//        
//        float fVer = [ currentVersion floatValue];
//        if ( fVer >= 4.2f ) {
//            [self initiAd:y];
//        }
//        
//        
//    }
    
}

- (void)hideAdmob:(BOOL)hide{
    if (adMobBanner) {
        adMobBanner.hidden = hide; 
        if (hide == NO) {
            [self.viewController.view bringSubviewToFront:adMobBanner];
            [UIView beginAnimations:@"BannerSlide" context:nil];
            [adMobBanner setFrame:CGRectMake(0, yPoint, 320, 50)];
            [UIView commitAnimations]; 
        }
    }
       
}

- (void)hideAllAds:(BOOL)hide{
    if (adMobBanner) {
        adMobBanner.hidden = hide; 
    }
    if (adBanner) {
        adBanner.hidden = hide;
    }

    if (adViewCtrl) {
        adViewCtrl.view.hidden = hide;
    }
    adHide = hide;
    
}


#pragma mark 아담광고 

- (void)addAdamAd:(CGFloat)y;
{
    CGRect frame = CGRectMake(0, 0, 320, 48);
    frame.origin.y = y + 2;    
    adViewCtrl = [[MobileAdViewController alloc] init];
    adViewCtrl.superViewCtrl = self.viewController;
    adViewCtrl.view.frame = frame;
    [viewController.view addSubview:adViewCtrl.view];
    
    
}

#pragma mark iAd 광고 
- (void)initiAd:(CGFloat)y{
    // iAd 배너
    if( NSClassFromString(@"ADBannerView") ) {
        adBanner = [[ADBannerView alloc] initWithFrame:CGRectZero];
        [adBanner setRequiredContentSizeIdentifiers:[NSSet setWithObjects:ADBannerContentSizeIdentifierPortrait, nil]];
        
        [adBanner setCurrentContentSizeIdentifier:ADBannerContentSizeIdentifierPortrait];
        [adBanner setFrame:CGRectMake(0, y, 320, 50)];
        [adBanner setDelegate:self];
        [viewController.view addSubview:adBanner];
        [adBanner release];        
    }
    
}

#pragma mark iAD Delegate

- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    NSLog(@"iAd 광고 수신성공");
    [UIView beginAnimations:@"animateBannerAppear" context:nil];
    [adBanner setFrame:CGRectMake(0, yPoint, 320, 50)];
    [UIView commitAnimations];
    iAdShow = YES;
    [self hideAdmob:YES];
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
    NSLog(@"iAd 광고 수신실패");
    [UIView beginAnimations:@"animateBannerOff" context:nil];
    [adBanner setFrame:CGRectMake(0, yPoint + 50, 320, 50)];
    [UIView commitAnimations];
    iAdShow = NO;
    [self hideAdmob:NO];
}


#pragma mark 애드몹 광고 
- (void)initAdmob:(CGFloat)y{
    // 애드몹 배너
    adMobBanner = [[GADBannerView alloc]
                   initWithFrame:CGRectMake(0, y + 50, 320, 50)];
    [adMobBanner setRootViewController:self.viewController];
    [adMobBanner setDelegate:self];
    [adMobBanner setAdUnitID:@"애드몹에서 발행된키"];
    [viewController.view addSubview:adMobBanner];
    [adMobBanner release];
    
    // 애드몹 광고 요청하기   
    GADRequest* requestAd = [GADRequest request];
    
    // 테스트 설정. 테스트 끝난 후 꼭 주석 처리   
    /*
    [requestAd setTestDevices:
     [NSArray arrayWithObjects:[[UIDevice currentDevice] uniqueIdentifier], nil]];
    */
    [adMobBanner loadRequest:requestAd];
}

#pragma mark adMob Delegate

- (void)adViewDidReceiveAd:(GADBannerView *)bannerView
{
    NSLog(@"애드몹 광고 수신성공");
    //광고 수신에 성공한 경우라도 현재 iad가 노출되어 있는 상태라면 노출시키지 않는다. 
    if (bannerView) {
        if (iAdShow == NO) {
            NSLog(@"아이애드 노출안되는 상태 애드몹 광고 올림");
            [UIView beginAnimations:@"BannerSlide" context:nil];
            [adMobBanner setFrame:CGRectMake(0, yPoint, 320, 50)];
            [UIView commitAnimations];        
        }        
    }
    else{
        [UIView beginAnimations:@"BannerSlide" context:nil];
        [adMobBanner setFrame:CGRectMake(0, yPoint, 320, 50)];
        [UIView commitAnimations];        
    }
    
}

- (void)adView:(GADBannerView *)bannerView didFailToReceiveAdWithError:(GADRequestError *)error
{
    NSLog(@"애드몹 광고 수신실패");
    [UIView beginAnimations:@"BannerSlide" context:nil];
    [adMobBanner setFrame:CGRectMake(0, yPoint + 50, 320, 50)];
    [UIView commitAnimations];
} 






@end
